import { login, getInfo, getMypermissions } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: ''
  },
  mutations: {
    set_token(store, token) {
      store.token = token
      setToken(token)
    },
    // 保存用户信息
    getInfo(store, data) {
      console.log(17, data)
      store.userInfo = data
    },
    // 清空用户信息
    delUserInfo(state) {
      state.userInfo = {}
    },
    // 清空token
    delToken(state) {
      state.token = ''
      removeToken()
    }

  },
  actions: {
    async login(context, data) {
      console.log(data)
      const res = await login(data)
      console.log(res)
      context.commit('set_token', res.data.jwt)
    },
    // 获取用户信息
    async getUserInfo(context) {
      const res = await getInfo()
      // 获取当前用户权限
      const res1 = await getMypermissions()
      // console.log(26, res.data)
      await context.commit('getInfo', { ...res.data, ...res1.data })
      // getMypermissions
    },
    // 退出登录的方法
    logout(store) {
      store.commit('delToken')
      store.commit('delUserInfo')
    }
  }
}

